/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.controller;

import java.sql.Connection;
import java.util.Collection;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.core.enums.DayOfWeek;
import org.aergia.vinny.database.pojo.Card;
import org.aergia.vinny.database.pojo.OperatingHours;
import org.aergia.vinny.database.pojo.Scan;
import org.aergia.vinny.database.pojo.Service;
import org.aergia.vinny.database.pojo.ServiceType;
import org.aergia.vinny.database.pojo.Skill;
import org.aergia.vinny.database.pojo.VinnyOption;
import org.aergia.vinny.database.pojo.VinnyRole;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.database.pojo.Volunteer;
import org.aergia.vinny.database.pojo.VolunteerGroup;
import org.aergia.vinny.database.pojo.Workstation;
import org.aergia.vinny.engine.Engine;
import org.aergia.vinny.gui.view.VinnyFrame;
import org.aergia.vinny.util.DateUtil;

/**
 * @author Fred
 */
public final class VinnyControllerImpl implements VinnyController {

	private final Engine engine;
	private VinnyFrame vinnyFrame;

	/**
	 * @param engine
	 */
	public VinnyControllerImpl(final Engine engine) {
		this.engine = engine;
	}

	@Override
	public ApiResult<VolunteerGroup> addGroup(final String groupName, final VinnyUser user) {
		return this.engine.addGroup(groupName, user);
	}

	/**
	 * @param cardNumber
	 */
	@Override
	public void cardScanned(final String cardNumber) {
		final ApiResult<Scan> result = this.engine.scanCard(cardNumber, DateUtil.getInstance());
		this.vinnyFrame.showResult(result);
	}

	@Override
	public ApiResult<VolunteerGroup> deleteGroup(final VolunteerGroup group, final VinnyUser user) {
		return this.engine.removeGroup(group, user);
	}

	@Override
	public ApiResult<ServiceType> deleteServiceType(final ServiceType serviceType, final VinnyUser user) {
		return this.engine.removeServiceType(serviceType, user);
	}

	@Override
	public ApiResult<Workstation> deleteWorkstation(final Workstation workstation, final VinnyUser user) {
		return this.engine.removeWorkstation(workstation, user);
	}

	@Override
	public ApiResult<Card> getCard(final String cardNumber) {
		return this.engine.getCard(cardNumber);
	}

	/**
	 * @return list of cards, it maybe empty
	 */
	@Override
	public Collection<? extends Card> getCards() {
		return this.engine.getCards();
	}

	@Override
	public Connection getDatabaseConnection() {
		return this.engine.getDatabase().getConnection();
	}

	/**
	 * @param groupName
	 * @return the result of getting the group
	 */
	@Override
	public ApiResult<VolunteerGroup> getGroup(final String groupName) {
		return this.engine.getGroup(groupName);
	}

	@Override
	public Collection<? extends VolunteerGroup> getGroups() {
		return this.engine.getGroups();
	}

	@Override
	public OperatingHours getHours(final DayOfWeek day) {
		return this.engine.getHours(day);
	}

	/**
	 * @param option
	 * @param defaultValue
	 * @return the the option or the default value if not set
	 */
	@Override
	public String getOption(final String option, final String defaultValue) {
		return this.engine.getOption(option, defaultValue);
	}

	@Override
	public Collection<? extends VinnyOption> getOptions() {
		return this.engine.getOptions();
	}

	@Override
	public VinnyRole getRole(final String permission) {
		final ApiResult<VinnyRole> result = this.engine.getRole(permission);
		return !result.isError() ? result.getResult() : null;
	}

	@Override
	public Collection<? extends Scan> getScans() {
		return this.engine.getScans();
	}

	@Override
	public Collection<? extends Service> getServices() {
		return this.engine.getServices();
	}

	/**
	 * @return a list of service types
	 */
	@Override
	public Collection<? extends ServiceType> getServiceTypes() {
		return this.engine.getServiceTypes();
	}

	/**
	 * @param skillName
	 * @return the skill
	 */
	@Override
	public ApiResult<Skill> getSkill(final String skillName) {
		return this.engine.getSkill(skillName);
	}

	@Override
	public Collection<? extends VinnyUser> getUsers() {
		return this.engine.getUsers();
	}

	@Override
	public Collection<VinnyUser> getUsers(final String inRole) {
		return this.engine.getUsers(inRole);
	}

	@Override
	public Collection<? extends Volunteer> getVolunteers() {
		return this.engine.getVolunteers();
	}

	@Override
	public Collection<? extends Workstation> getWorkstations() {
		return this.engine.getWorkstations();
	}

	@Override
	public ApiResult<Card> isCardAvailable(final Volunteer volunteer, final String cardNumber) {
		return this.engine.isCardAvailable(volunteer, cardNumber);
	}

	@Override
	public boolean isUserNameInUse(final String userName) {
		return this.engine.isUserNameInUse(userName);
	}

	@Override
	public boolean isVinnyCard(final String cardNumber) {
		return this.engine.isVinnyCard(cardNumber);
	}

	@Override
	public ApiResult<Card> saveCard(final Card card, final VinnyUser user) {
		return this.engine.saveCard(card, user);
	}

	@Override
	public ApiResult<VolunteerGroup> saveGroup(final VolunteerGroup group, final VinnyUser user) {
		return this.engine.saveGroup(group, user);
	}

	@Override
	public ApiResult<OperatingHours> saveHours(final OperatingHours hours, final VinnyUser user) {
		return this.engine.saveHours(hours, user);
	}

	@Override
	public ApiResult<VinnyOption> saveOption(final VinnyOption option, final VinnyUser user) {
		return this.engine.saveOption(option, user);
	}

	@Override
	public ApiResult<Scan> saveScan(final Scan scan, final VinnyUser user) {
		return this.engine.saveScan(scan, user);
	}

	@Override
	public ApiResult<Service> saveService(final Service service, final VinnyUser user) {
		return this.engine.saveService(service, user);
	}

	@Override
	public ApiResult<ServiceType> saveServiceType(final ServiceType serviceType, final VinnyUser user) {
		return this.engine.saveServiceType(serviceType, user);
	}

	@Override
	public ApiResult<VinnyUser> saveUser(final VinnyUser saveUser, final VinnyUser user) {
		return this.engine.saveUser(saveUser, user);
	}

	@Override
	public ApiResult<Volunteer> saveVolunteer(final Volunteer volunteer, final VinnyUser user) {
		return this.engine.saveVolunteer(volunteer, user);
	}

	@Override
	public ApiResult<Workstation> saveWorkstation(final Workstation workstation, final VinnyUser user) {
		return this.engine.saveWorkstation(workstation, user);
	}

	/**
	 * @param vinnyFrame
	 */
	@Override
	public void setVinnyFrame(final VinnyFrame vinnyFrame) {
		this.vinnyFrame = vinnyFrame;
	}

	@Override
	public ApiResult<Card> switchCard(final Card oldCard, final Card newCard) {
		return this.engine.switchCard(oldCard, newCard);
	}

	@Override
	public ApiResult<VinnyUser> validateUser(final String userName, final String password) {
		return this.engine.validateUser(userName, password);
	}
}
